//
//  YZYAlertView.h
//  YZYSDKManagerDemo
//
//  Created by 赵隆杰 on 2024/6/7.
//

#import "YZYAlertView.h"
#import "YZYSDKManagerHeader.h"
#import "Masonry.h"
#import "YZYSaccountStore.h"
#import "YZYSDKManager.h"
#import "ZCFunDataFormat.h"

@interface YZYAlertView ()

@property (nonatomic, strong) UIButton *coverView;
@property (nonatomic ,strong) UIButton *loginBtn;

@end

@implementation YZYAlertView

- (instancetype)init {
    if (self == [super init]) {
        
        UIImageView *backbg = [[UIImageView alloc] init];
        backbg.userInteractionEnabled = YES;
        backbg.backgroundColor = [UIColor whiteColor];
        backbg.layer.masksToBounds = YES;
        backbg.layer.cornerRadius = 4;
        [self addSubview:backbg];
        backbg.frame = CGRectMake(0, 0, popupW, popupH_short);
        
        UILabel *titleLabel = [[UILabel alloc] init];
        titleLabel.font = [UIFont boldSystemFontOfSize:24];
        titleLabel.textAlignment = NSTextAlignmentCenter;
        titleLabel.text = @"提示";
        [backbg addSubview:titleLabel];
        [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(backbg).offset(15);
            make.centerX.equalTo(backbg);
        }];
        
        UILabel *contentLabel = [[UILabel alloc] init];
        contentLabel.numberOfLines = 0;
        [backbg addSubview:contentLabel];
        contentLabel.text = @"登录已失效，请重新登录";
        contentLabel.textColor = [UIColor colorWithRed:185/255.0 green:185/255.0 blue:185/255.0 alpha:1.0];
        contentLabel.font = [UIFont systemFontOfSize:14];
        [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(titleLabel.mas_bottom).offset(20);
            make.centerX.equalTo(backbg);
        }];

        self.loginBtn = [[UIButton alloc] init];
        self.loginBtn.layer.cornerRadius = 5;
        self.loginBtn.layer.masksToBounds = YES;
        self.loginBtn.backgroundColor = yzy_themeColor;
        self.loginBtn.titleLabel.font = [UIFont systemFontOfSize:16];
        [self.loginBtn setTitle:@"好  的" forState:UIControlStateNormal];
        [self.loginBtn addTarget:self action:@selector(didClickLogin) forControlEvents:UIControlEventTouchUpInside];
        [backbg addSubview:self.loginBtn];
        [self.loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(backbg);
            make.left.equalTo(backbg).offset(25);
            make.height.mas_equalTo(40);
            make.bottom.equalTo(backbg).offset(-20);
        }];
        
    }
    return self;
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    [super touchesBegan:touches withEvent:event];
    [self endEditing:YES];
}

- (void)didClickLogin {
    
    //清除登录信息
    [[YZYSaccountStore sharedInstance] removeAccessToken];
    [[YZYSaccountStore sharedInstance] removeUserInfoStr];
    [[YZYSaccountStore sharedInstance] removeUserIdentifier];
    
    //返回登录页
    [[YZYSDKManager shareManager] yzy_showLoginUI];
    
    [self closeView];
}


- (void)show {
    [[YZYSaccountStore sharedInstance] getCurrentWindowWithCompletion:^(UIWindow *window) {
        if (window) {
            UIButton *coverView = [[UIButton alloc] init];
            coverView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3f];
            self.coverView = coverView;
            coverView.frame = window.bounds;
            
            UIBlurEffect *blurEffect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
            UIVisualEffectView *visualView = [[UIVisualEffectView alloc] initWithEffect:blurEffect];
            visualView.alpha = 0.0;
            [coverView addSubview:visualView];
            visualView.frame = coverView.frame;
            
            [window addSubview:coverView];
            
            self.frame = CGRectMake(0, 0, popupW, popupH_short);
            self.layer.cornerRadius = 12.0f;
            self.center = CGPointMake(window.center.x, window.center.y);
            if (![ZCFunDataFormat GetIsLandscape]) {
                self.center = CGPointMake(window.center.x, window.center.y - 40);
            }
            [window addSubview:self];
        }
    }];
}

- (void)closeView {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self.coverView removeFromSuperview];
        [self removeFromSuperview];
    });
}

@end
